package atguigu.jdbc;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.sql.*;
import java.sql.Date;
import java.util.*;

@Slf4j
public abstract class DaoHandle {
    protected abstract Connection getDao() throws IOException, PropertyVetoException, SQLException;

    public String queryForStringHandle(String sql, Object... objects) throws Exception {
        PreparedStatement stmt = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            conn = getDao();
            stmt = conn.prepareStatement(sql);

            setPreparedStatementParams(stmt, objects);

            rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getString(1);
            }

        } catch (SQLException e) {
            log.error("执行sql语句错误：" + sql, e);
            throw new Exception(e.getLocalizedMessage());
        } finally {
            if (stmt != null)
                releaseSqlConnection(rs, stmt, null, conn);
        }
        return null;
    }

    public int executeSqlHandle(String sql, Object... objects) throws Exception {
        PreparedStatement stmt = null;
        Connection conn = null;
        try {
            conn = getDao();
            stmt = conn.prepareStatement(sql);
            setPreparedStatementParams(stmt, objects);
            return stmt.executeUpdate();
        } catch (SQLException e) {
            log.error("执行sql语句错误：" + sql, e);
            throw new Exception(e.getLocalizedMessage());
        } finally {
            releaseSqlConnection(null, stmt, null, conn);
        }
    }

    public static void setPreparedStatementParams(PreparedStatement stmt, Object[] objects) throws SQLException {
        for (int i = 1; i <= objects.length; i++) {
            Object obj = objects[i - 1];
            if (obj == null) {
                stmt.setString(i, "");
                continue;
            }
            if (obj instanceof String) {
                stmt.setString(i, (String) obj);
            } else if (obj instanceof Timestamp) {
                stmt.setTimestamp(i, (Timestamp) obj);
            } else if (obj instanceof Date) {
                stmt.setDate(i, (Date) obj);
            } else if (obj instanceof Integer) {
                stmt.setInt(i, ((Integer) obj).intValue());
            } else if (obj instanceof Double) {
                stmt.setDouble(i, ((Double) obj).doubleValue());
            } else if (obj instanceof Long) {
                stmt.setLong(i, ((Long) obj).longValue());
            } else if (obj instanceof BigDecimal) {
                stmt.setBigDecimal(i, ((BigDecimal) obj));
            }
        }
    }

    public static void releaseSqlConnection(ResultSet rSet, PreparedStatement pStatement, Statement iStatement, Connection connection) {
        try {
            if (rSet != null) {
                rSet.close();
            }
        } catch (SQLException e) {
            log.error("System error", e);
        } finally {
            try {
                if (pStatement != null) {
                    pStatement.close();
                }
                if (iStatement != null) {
                    iStatement.close();
                }
            } catch (SQLException e) {
                log.error("System error", e);
            } finally {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    log.error("System error", e);
                }
            }
        }
    }
}
